package com.enlorenz.core.queue.proc;

import java.util.Date;

import com.enlorenz.common.util.CommonUtils;
import com.enlorenz.core.engine.AbstractBuzHandler;
import com.enlorenz.core.engine.imp.thread.ControlThread;

/**
 * 接收驱动的休眠线程
 * 如果有结果则不断处理，没有则进行sleep
 * @author lcb
 */
public abstract class AbstractRecvDriveSleepThread extends ControlThread{
	//休眠时间
	private long sleepTime;
	{
		
		buzHandler=new AbstractBuzHandler() {
			
			@Override
			protected void onFinallyDo() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			protected void onExceptionHappened(Exception e) {
				processOver();
			}
			
			/**
			 * 线程开始必须做的
			 * @throws Exception
			 */
			protected void doWhenStart() throws Exception{
				
			}
			
			@Override
			protected void buzHandleImp() throws Exception {
				Object object = getRecvMsg();
				if (null != object) {
					trans2Imp(object);
					//加入结束状态
					threadEndDate=new Date();
				}else{
					threadEndDate=new Date();
					processOver();
				}
			}
		};
	}
	
	/**
	 * 获取休眠时间参数
	 * @return 休眠时间参数
	 */
	public long getSleepTime() {
		return sleepTime;
	}
	
	/**
	 * 设置休眠时间参数
	 * @param sleepTime 休眠时间参数
	 */
	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}
	
	/**
	 * 返回收到的信息
	 * @return
	 */
	protected abstract Object getRecvMsg();
	/**
	 * 业务处理结束
	 */
	protected void processOver(){
		CommonUtils.sleep(sleepTime);
	}
	
	/**
	 * 处理接收到的结果
	 * @param resultList
	 */
	protected abstract void trans2Imp(Object object);
	
}
